import sys,os
import requests
import threading
from PyQt5.QtWidgets import *
from PyQt5.QtCore import pyqtSlot
import PyQt5.QtGui as qg
import time  # 引入时间
import win32api
import win32con
import win32clipboard
from Ui_Ui import *
#主窗口
class MainWindow(QMainWindow,Ui_MainWindow):
    def __init__(self):
      super(MainWindow,self).__init__()
      self.setupUi(self)
      self.flag = 0 #线程初始状态

    def thread(self): #线程
        t = threading.Thread(target=self.on)
        t.start()
        
    def on(self): #开始
        self.flag = 1
        print("线程开启")
        
        while True:
            if self.flag == 1:
              text = self.textEdit.toPlainText().split('\n')
              for i in text:
                if self.label.text()=='暂停ing...':
                  self.flag == 0
                else:
                  time.sleep(int(self.spinBox.text()))
                  print(i)
                  # 按下ctrl+A
                  win32api.keybd_event(0x11, 0, 0, 0)
                  win32api.keybd_event(0x41, 0, 0, 0)
                  win32api.keybd_event(0x41, 0, win32con.KEYEVENTF_KEYUP, 0)
                  win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
                  #复制到剪切板
                  win32clipboard.OpenClipboard()
                  win32clipboard.EmptyClipboard()
                  win32clipboard.SetClipboardText(i)
                  win32clipboard.CloseClipboard()
                  #从剪切板粘贴
                  win32api.keybd_event(0x11, 0, 0, 0)
                  win32api.keybd_event(0x56, 0, 0, 0)
                  win32api.keybd_event(0x56, 0, win32con.KEYEVENTF_KEYUP, 0)
                  win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
                  time.sleep(1)
                  # 按下回车
                  win32api.keybd_event(0x0D, 0, 0, 0)
                  win32api.keybd_event(0x0D, 0, win32con.KEYEVENTF_KEYUP, 0)
            else:
                break
        print("暂停成功！")

    def off(self):  #暂停
        self.flag = 0
        #print('22222')
    @pyqtSlot()  #限制器，限制槽函数触发两次 
    #开始按钮
    def on_pushButton_clicked(self):
      self.thread()
      self.label.setText('正在运行...')



    @pyqtSlot()  #限制器，限制槽函数触发两次 
    #暂停按钮
    def on_pushButton_2_clicked(self):
      self.off()
      self.label.setText('暂停ing...')


    #鸡汤模式
    @pyqtSlot()  #限制器，限制槽函数触发两次 
    def on_checkBox_2_clicked(self):
      if self.checkBox_2.isChecked() == True:
        self.label.clear()
        url = 'https://api.oick.cn/dutang/api.php'
        for i in range(1,11):
          text = requests.get(url).text.replace('"', '')
          self.textEdit.append(text)
      else:
        self.textEdit.clear()


    #舔狗模式
    @pyqtSlot()  #限制器，限制槽函数触发两次 
    def on_checkBox_clicked(self):
      self.label.clear()
      if self.checkBox.isChecked() == True:
        url = 'https://api.oick.cn/dog/api.php'
        for i in range(1,11):
          text = requests.get(url).text.replace('"', '')
          self.textEdit.append(text)
      else:
        self.textEdit.clear()



    #增加数据
    @pyqtSlot()  #限制器，限制槽函数触发两次 
    def on_toolButton_2_clicked(self):
      #舔狗模式被勾选，增加舔狗数据
      if self.checkBox.isChecked() == True:
        url = 'https://api.oick.cn/dog/api.php'
        for i in range(1,11):
          text = requests.get(url).text.replace('"', '')
          self.textEdit.append(text)
          self.label.setText('数据已添加')
      #鸡汤模式被勾选，增加鸡汤数据
      elif self.checkBox_2.isChecked() == True:
        url = 'https://api.oick.cn/dutang/api.php'
        for i in range(1,11):
          text = requests.get(url).text.replace('"', '')
          self.textEdit.append(text)
          self.label.setText('数据已添加')
      else:
        self.label.setText('请选择模式')


    #导入文件
    @pyqtSlot()  #限制器，限制槽函数触发两次 
    def on_toolButton_clicked(self):
      fileName,fileType = QtWidgets.QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(), "Text Files(*.txt)")
      if not os.path.exists(fileName):
        pass
      else:
        fileText = open(fileName, 'r', encoding="utf-8")
        my_data = fileText.read()
        self.textEdit.setPlainText(my_data)
        print(my_data)
        print(fileName)
        print(fileType)


if __name__ == '__main__':
  app = QApplication(sys.argv)
  win = MainWindow()
  win.setWindowIcon(qg.QIcon("./img/icon.ico"))
  win.show()
  sys.exit(app.exec_())      